<?php


Loader::loadClass('DateUtilEx', "modules/POBPortal/pnincludes");
Loader::loadClass('ObjectUtilEx', "modules/POBPortal/pnincludes");
Loader::loadClass('PNObjectEx', "modules/POBPortal/pnincludes");
Loader::loadClass('PNObjectExArray', "modules/POBPortal/pnincludes");
Loader::loadClass('DBUtilEx', "modules/POBPortal/pnincludes");
Loader::loadClass('SecurityUtilEx', "modules/POBPortal/pnincludes");
Loader::loadClass('InnoUtil', "modules/POBPortal/pnincludes");

/**
 * initialise block
 * 
 * @author       The PostNuke Development Team
 */
function POBPortal_RecommendHotelblock_init()
{

}

/**
 * get information on block
 * 
 * @author       The PostNuke Development Team
 * @return       array       The block information
 */
function POBPortal_RecommendHotelblock_info()
{
    return array('text_type'      => 'Recommend Hotel',
                 'module'         => 'POBPortal',
                 'text_type_long' => 'Show recommend hotels',
                 'allow_multiple' => true,
                 'form_content'   => false,
                 'form_refresh'   => false,
                 'show_preview'   => true);
}

/**
 * display block
 * 
 * @author       The PostNuke Development Team
 * @param        array       $blockinfo     a blockinfo structure
 * @return       output      the rendered bock
 */
function POBPortal_RecommendHotelblock_display($blockinfo)
{
    $modname  = 'POBPortal';
    $class    = 'RecommendHotel';

    $modinfo = pnModGetInfo(pnModGetIDFromName($modname));
    $directory = $modinfo['directory'];
    pnModDBInfoLoad($modname, $directory);


    //Load language
    $lang = pnUserGetLang();
    if (file_exists('modules/POBPortal/pnlang/' . $lang . '/user.php')){
      Loader::loadFile('user.php', 'modules/POBPortal/pnlang/' . $lang );
    }else if (file_exists('modules/POBPortal/pnlang/eng/user.php')){
      Loader::loadFile('user.php', 'modules/POBPortal/pnlang/eng' );
    }

    //load render
    $render = pnRender::getInstance($modname);

    ///////////////////////////////////////////////
    //
    // Search recommend hotel
    //
    ///////////////////////////////////////////////
    
    //Call setting block variable
    $vars = pnBlockVarsFromContent($blockinfo['content']);    

    //Get all hotel is activated    
    $args = array("callForm"=>"POBPortal");
    $memberActivateHotel = pnModAPIFunc('POBMember', 'user', 'getAmountActivateHotel', $args);
    
    //Get all hotel is activated  from block setting
    $settingAmountActivateHotel = isset($vars['amount'])?$vars['amount']:100;
      
    $amountActivateHotel = isset($memberActivateHotel)? $memberActivateHotel : $settingAmountActivateHotel ;    
    
    //get limit from block setting    
    $settingLimit = isset($vars['pagesize'])?$vars['pagesize']:10;
    
    //get limit from URL
    $getLimit  = FormUtil::getPassedValue ('limit', false); 
    
    //Select limit       
    $limit = (isset($getLimit) && $getLimit )? $getLimit : $settingLimit;
    
    //echo $limit.":".$amountActivateHotel; exit;
    
        
    $offset = FormUtil::getPassedValue ('offset', 0);    
		$amount_page = ceil($amountActivateHotel/$limit); 
    $location  = "phuket";
    $distance  = "50";
    $latitude  = "7.970838";
    $longitude = "98.329697";
		$startDate = date("Y-m-d");
    $endDate   = date("Y-m-d", mktime(0, 0, 0, date("m")  , date("d")+1, date("Y")));   
		
		

    //Send param to HotelSearch service 
    Loader::loadClass('HotelLimitSearchEndpoint',"modules/POBPortal/pnincludes");
    $hotelLimitSearch = new HotelLimitSearchEndpoint();
    $hotelLimitSearch->setHotelLimitSearchXML( $location, $distance, $latitude, $longitude, $startDate, $endDate, $limit, $offset);

    //XML Response
    $response = $hotelLimitSearch->getHotelLimitSearchXML();
    //print($response);exit;
    $xmlObject = simplexml_load_string($response);


		$twoCharacterLanguage = $lang[0].$lang[1];
		//print($twoCharacterLanguage); exit;
    $repackObjectArray = $hotelLimitSearch->repackObjectArrayForDisplay($xmlObject, $twoCharacterLanguage);
    $repackArray = $repackObjectArray["Properties"];


		//Check minimum rate
		$minRate = 0;
    for($i=0; $i<count($repackArray); $i++){
			$minRate = $repackArray[$i]['Availabilities'][0]['Rate'];    
    	for($j=0; $j<count($repackArray[$i]['Availabilities']); $j++){
				if($repackArray[$i]['Availabilities'][$j]['Rate'] < $minRate ){
					$minRate = $repackArray[$i]['Availabilities'][$j]['Rate'];			
				}			
			}  
			unset($repackArray[$i]['Availabilities']);				
			$repackArray[$i]['Rate'] = $minRate;  	
    }
    
    //print_r($repackArray); exit;
    $render->assign("startDate", $startDate );
    $render->assign("endDate", $endDate );
    $render->assign("Latitude", $latitude );
    $render->assign("Longitude", $longitude );
    $render->assign("objectArray", $repackArray );
    $render->assign("amount_page", $amount_page );    
    $render->assign("currentLanguage", $lang ); 
    $render->assign("limit", $limit );     
     
    // Populate block info and pass to theme
    $blockinfo['content'] = $render->fetch('block_recommendhotel.htm');
    return themesideblock($blockinfo);
}
/**
 * modify block settings
 * 
 * @author       The PostNuke Development Team
 * @param        array       $blockinfo a blockinfo structure
 * @return       output      the bock form
 */
function POBPortal_RecommendHotelblock_modify($blockinfo)
{
  $vars = pnBlockVarsFromContent($blockinfo['content']);
  //load render
  $render = pnRender::getInstance('POBPortal');
  $render->assign('form', $vars);
  
	return $render->fetch('block_recommendhotel_modify.htm');
}


/**
 * update block settings
 * 
 * @author       The PostNuke Development Team
 * @param        array       $blockinfo     a blockinfo structure
 * @return       $blockinfo  the modified blockinfo structure
 */
function POBPortal_RecommendHotelblock_update($blockinfo)
{
    
    // Get current content
    $vars = pnBlockVarsFromContent($blockinfo['content']);
    
    // alter the corresponding variable
    $form = FormUtil::getPassedValue ('form', false );
    $vars['pagesize'] = !empty($form['pagesize']) ? $form['pagesize'] : 10;
    $vars['amount']   = !empty($form['amount']) ? $form['amount'] : 100;    

    // write back the new contents
    $blockinfo['content'] = pnBlockVarsToContent($vars);

    // clear the block cache
    $pnRender = pnRender::getInstance('POBPortal');
    $pnRender->clear_cache('block_recommendhotel.htm');
    
    return $blockinfo;
}
